<?php
namespace app\home\controller;
session_start();
use think\Db;
use think\captcha\Captcha;
use think\Loader;
class Login extends \think\Controller{
    public function login(){
        if(isset($_POST['put'])){
            $username=input('param.username','','htmlspecialchars');
            $pwd=input('param.pwd','','htmlspecialchars');
            $res=db('user')->where('username',$username)->find();         
            if($username==$res['username']&&$pwd==$res['pwd']){
                session('name',$res['username']);
                session('pic',$res['avatar']);
                session('id',$res['id']);
                $this->success('登录成功!','home/myqrcode/myqrcode');
            }
            else {
                $this->error('登录失败');
            }
        }

        $appid="3911964253";
        $redirect_uri="http://wx.weiyinstudio.com?backurl=http://www.b2c.com/index.php/home/login/weibo?";
        $wb_url="https://api.weibo.com/oauth2/authorize?client_id={$appid}&response_type=code&redirect_uri={$redirect_uri}";
        $this->assign('wb_url',$wb_url);
        return $this->fetch('login');//继承基类的控制器 
    }
    public function weibo(){
        $code=input('get.code');
        $appid="3911964253";
        $client_secret="7ebbd60b716a6163e8108d9e12109de8";
        $redirect_uri="http://wx.weiyinstudio.com?backurl=http://www.b2c.com/index.php/home/login/weibo?";
        $wb_url="https://api.weibo.com/oauth2/access_token?client_id={$appid}&client_secret={$client_secret}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
        $token=post($wb_url,array());
        $token=json_decode($token,true);
        if($token==false){
            $this->error('access_token获取失败!');
        }
        // var_dump($token);
        // echo "<pre>";

        $access_token=$token['access_token'];
        $uid=$token['uid'];
        $wb_url2="https://api.weibo.com/2/users/show.json?access_token={$access_token}&uid={$uid}";
        $info=get($wb_url2,array());
        if($info==false){
            $this->error('用户信息获取失败!');
        }
        $info=json_decode($info,true);
        // var_dump($info);

        session('info',$info);
        $res=db('user')->where("weibo_uid={$info['id']}")->find();  

        // $id=$info['id'];
        // $res=Db::name('user')->where('weibo_uid',$id)->find();  
        if(empty($res)){
            // $_SESSION['user_id']=$res['id'];
            // $this->redirect('home/login/bind');
            $this->success('请绑定手机号码!','home/login/bind');
        }else{
            // $_SESSION['weibo_uid']=$id;
            // $_SESSION['user_id']=$res['id'];
            // echo "123";
            session('avatar',$res['avatar']);
            session('name',$res['id']);
            session('name',$res['name']);
            $this->success('登录成功!','home/index/index');
        }
    }
    public function bind(){
        return $this->fetch('bind');
       
    }
    public function checkbind(){
    
        $info=session('info');
        $tell=input('param.tell','','htmlspecialchars');
        $data=[
            'tell'=> $tell,
            'weibo_uid'=>$info['id'],
            'avatar'=>$info['avatar_hd'],
            'name'=>$info['screen_name'],
            'sex'=>$info['gender'],
            'time'=>time(),
        ];
        $checkTell=db('user')->where("tell={$tell}")->find();
        if(empty($checkTell)){
            $ret=db('user')->insertGetId($data);
            if($ret>0){
                session('name',$info['screen_name']);
                $this->success('绑定成功!','home/index/index');
            }else{
                $this->error('绑定失败!');
            }
        }else{
        // $checkTell=db('user')->where("tell=$tell")->find();
            $ret=db('user')->where('tell',$tell)->update($data);
            if($ret>0){
                $this->success('更新绑定成功!','home/index/index');
            }else{
                $this->error('绑定失败!');
            }
        }
    
        
    }
    // public function sms(){
    //     $tell=input('param.tell','','htmlspecialchars');
    //     $tellCode=mt_rand(100000,999999);
    //     $_SESSION['code']=$tellCode;
    //     $_SESSION['tell']=$tell;
    //     Vendor('topthink.aliyun.sendSms');
    //     $ret=sendSms($tell,$tellCode);
    //     echo '成功';
    // }
    
}